<template>
  <div class="recommend" v-loading:[loadingText]="loading">
    <scroll class="recommend-content">
      <div>
        <div class="slider-wrapper">
          <div class="slider-content">
            <slider v-if="sliders.length" :sliders="sliders"></slider>
          </div>
        </div>
        <div class="recommend-list">
          <h1 class="list-title" v-show="!loading">热门歌单推荐</h1>
          <ul>
            <li
              v-for="item in albums"
              class="item"
              :key="item.dissid"
              @click="selectItem(item)"
            >
              <div class="icon">
                <img width="60" height="60" v-lazy="item.imgurl">
              </div>
              <div class="text">
                <h2 class="name">
                  {{ item.creator.name }}
                </h2>
                <p class="title">
                  {{item.dissname}}
                </p>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </scroll>
    <router-view v-slot="{ Component }">
      <transition appear name="slide">
        <component :data="selectedAlbum" :is="Component"></component>
      </transition>
    </router-view>
  </div>
</template>

<script>
  import { getBannerList, getRecommendList } from '@/service/recommend'
  import Slider from '@/components/base/slider/slider'
  import Scroll from '@/components/wrap-scroll/index'
  import storage from 'good-storage'
  import { ALBUM_KEY } from '@/assets/js/constant'

  export default {
    name: 'recommend',
    components: {
      Slider,
      Scroll
    },
    data() {
      return {
        sliders: [],
        albums: [],
        selectedAlbum: null,
        loadingText: '正在载入'
      }
    },
    computed: {
      loading() {
        return !this.sliders.length && !this.albums.length
      }
    },
    async created() {
      const result = await getBannerList()
      this.sliders = result.slider
      const res = await getRecommendList()
      this.albums = res.list
    },
    methods: {
      selectItem(album) {
        this.selectedAlbum = album
        this.cacheAlbum(album)
        this.$router.push({
          path: `/recommend/${album.dissid}`
        })
      },
      cacheAlbum(album) {
        storage.session.set(ALBUM_KEY, album)
      }
    }
  }
</script>

<style lang="scss">
  .recommend {
    position: fixed;
    width: 100%;
    top: 88px;
    bottom: 0;
    overflow: scroll;
    overflow-x: hidden;
    z-index: 100;
    .recommend-content {
      height: 100%;
      overflow: hidden;
      .slider-wrapper {
        position: relative;
        width: 100%;
        height: 0;
        padding-top: 40%;
        overflow: hidden;
        .slider-content {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
        }
      }
      .recommend-list {
        .list-title {
          height: 65px;
          line-height: 65px;
          text-align: center;
          font-size: 14px;
          color: $theme-color;
        }
        .item {
          display: flex;
          box-sizing: border-box;
          align-items: center;
          padding: 0 20px 20px 20px;

          .icon {
            flex: 0 0 60px;
            width: 60px;
            padding-right: 20px;
          }
          .text {
            display: flex;
            flex-direction: column;
            justify-content: center;
            flex: 1;
            line-height: 20px;
            overflow: hidden;
            font-size: 14px;
          }
          .name {
            margin-bottom: 10px;
            color: $theme-color;
          }
          .title {
            color: #999;
          }
        }
      }
    }
  }
</style>
